package stud.mpgame.flash.game;

import flash.Lib;
import stud.mpgame.snapshot.SnapshotManager;

import flash.ui.Keyboard;

import flash.events.KeyboardEvent;

import stud.mpgame.network.NetworkMonitor;
import stud.mpgame.network.Message;

import stud.mpgame.network.service.time.TimeService;

import stud.mpgame.flash.net.command.ArrowKeyCommand;

import stud.mpgame.flash.net.description.ArrowKeyMessageDescription;
	
class ArrowKeyListener {

	private var networkMonitor:NetworkMonitor;
	private var timeService:TimeService; //TODO WRONG
	private var snapshotManager:SnapshotManager; //TODO WRONG
	
	private var pressed:Array<Bool>;	
	
	public function new(networkMonitor:NetworkMonitor, timeService:TimeService, snapshotManager:SnapshotManager) {
		this.networkMonitor = networkMonitor;
		this.timeService = timeService;
		this.snapshotManager = snapshotManager;
		
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedListener);	
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, keyReleasedListener);	  
		
		pressed = [false, false, false, false];
	}
	
	private function keyPressedListener(e:KeyboardEvent):Void  {
		var keyCode:Int = -1;
        if (e.keyCode == Keyboard.UP)
			keyCode = 0;
        if (e.keyCode == Keyboard.RIGHT)
			keyCode = 1;
        if (e.keyCode == Keyboard.DOWN)
			keyCode = 2;
        if (e.keyCode == Keyboard.LEFT)
			keyCode = 3;
		
		if (e.keyCode == 49) //key - 1
			snapshotManager.setMode(1); //only exterpolation
		else if ( e.keyCode == 50) //key - 2
			snapshotManager.setMode(2); //only interpolation
		else if ( e.keyCode == 192) //key - ~
			snapshotManager.setMode(0); //interpolation and exterpolation
			
        
		if (keyCode != -1) {
			if ( pressed[keyCode] )
				return;
			
			pressed[keyCode] = true;
			
			var time = snapshotManager.getNextApplyCommandTime();
			trace("Send command: " + snapshotManager.getLocalSnapshotTime() + "; " + time);
			var arrowKeyCommand = new ArrowKeyCommand(keyCode, true, time);
			snapshotManager.addCommand(arrowKeyCommand);
			var message:Message = new Message(networkMonitor, ArrowKeyMessageDescription.getInstance());
			message.putUserObject(arrowKeyCommand);
			message.send();
		}			
	}
	
	private function keyReleasedListener(e:KeyboardEvent):Void  {
		var keyCode:Int = -1;
        if (e.keyCode == Keyboard.UP)
			keyCode = 0;
        if (e.keyCode == Keyboard.RIGHT)
			keyCode = 1;
        if (e.keyCode == Keyboard.DOWN)
			keyCode = 2;
        if (e.keyCode == Keyboard.LEFT)
			keyCode = 3;
        
		if (keyCode != -1) {
			pressed[keyCode] = false;
			
			//почему-то с snapshotManager.getLocalSnapshotTime() не работает. Сервер применяет команду к пред. снепшоту. Разбираться лень.
			var time = snapshotManager.getNextApplyCommandTime();
			trace("Send command: " + snapshotManager.getLocalSnapshotTime() + "; " + time);
            var arrowKeyCommand = new ArrowKeyCommand(keyCode, false, time);
			snapshotManager.addCommand(arrowKeyCommand);
			var message:Message = new Message(networkMonitor, ArrowKeyMessageDescription.getInstance());
			message.putUserObject(arrowKeyCommand);
			message.send();
        }			
	}
	
	public function clear():Void {
		Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressedListener);	   
		Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleasedListener);	  
	}
	
}